package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.RegisterForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-09
 */
@RestController


@Api(tags="会员管理")
@RequestMapping("/api/ucenter/member")
public class ApiMemberController {

    @Autowired
    private MemberService memberService;

    @PostMapping("/regist")
    public R regist(@RequestBody RegisterForm registerForm){
        memberService.regist(registerForm);
        return R.ok().message("注册成功");
    }

    @GetMapping("/login")
    public R login(@RequestParam("mobile") String mobile,
                   @RequestParam("password") String password){
        String token = memberService.login(mobile,password);
        return R.ok().data("jwt",token);
    }

    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("jwtinfo",jwtInfo);

    }

    @GetMapping("getByMemberId/{memberId}")
    public R getByMemberId(@PathVariable String memberId){
        Member member = memberService.getById(memberId);
        MemberDto memberDto = new MemberDto(member.getId(),member.getNickname(),member.getMobile());
        return  R.ok().data("memberDto",memberDto);
    }

}

